package dfa;

import java.util.Set;

/**
 * @author haoly
 * @title DFA
 * @date 2025/4/16 12:00
 * @description DFA
 */
public class DFA <T>{
    private T currentState;

    private final Set<T> acceptStates;

    private final DFARulebook<T> rulebook;

    public DFA(T currentState, Set<T> acceptStates, DFARulebook<T> rulebook) {
        this.currentState = currentState;
        this.acceptStates = acceptStates;
        this.rulebook = rulebook;
    }

    public boolean accepting(){
        return acceptStates.contains(currentState);
    }

    public Set<T> getAcceptStates() {
        return acceptStates;
    }

    public T getCurrentState() {
        return currentState;
    }

    public DFARulebook<T> getRulebook() {
        return rulebook;
    }

    public void readCharacter(Character character){
        this.currentState = this.rulebook.nextState(this.currentState, character);
    }

    public void readString(String s){
        for (int i = 0; i < s.length(); i++) {
            Character character = s.charAt(i);
            readCharacter(character);
        }
    }
}
